package cn.edu.neusoft.wuguangsheng.flowerimpression.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.CartGoodsBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.CartAddGoodsNumModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.CartDescGoodsNumModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.service.Service;

public class BuyCartGoodsAdapter extends BaseAdapter<CartGoodsBean> {
    private Handler handler;

    public BuyCartGoodsAdapter(Context context, List<CartGoodsBean> items, int layoutResourse,Handler handler) {
        super(context, items, layoutResourse);
        this.handler = handler;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(context).inflate(layoutResourse,parent,false);
        BuyCartGoodsViewHolder viewHolder = new BuyCartGoodsViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final BuyCartGoodsViewHolder buyCartGoodsViewHolder=(BuyCartGoodsViewHolder) holder;
        final CartGoodsBean cartGoodsBean=items.get(position);

        final String goodsname = cartGoodsBean.getGood_name();
        final double goodsprice = cartGoodsBean.getGood_price();
        final int goodsnum = cartGoodsBean.getCart_goodnum();
        final String img = Service.BASE_URL+"flower_impression/public/uploads/"+cartGoodsBean.getGood_photo();
        final String carid = cartGoodsBean.getCart_id();
        final double xiaoji = goodsprice*goodsnum;

        buyCartGoodsViewHolder.buycartitem_goodsname.setText(goodsname);
        buyCartGoodsViewHolder.buycartitem_goodsprice.setText("￥"+goodsprice);
        buyCartGoodsViewHolder.buycartitem_goodsnum.setText(String.valueOf("X"+goodsnum));
        buyCartGoodsViewHolder.buycartitem_goodsxiaojie.setText(String.valueOf(xiaoji));
        buyCartGoodsViewHolder.buycartitem_goodsgmsl.setText(String.valueOf(goodsnum));
        buyCartGoodsViewHolder.buycartitem_gjjsp.setText("您选择了"+String.valueOf(goodsnum)+"份商品");

        Picasso.with(context).load(img).into(buyCartGoodsViewHolder.buycartitem_goodsimg);

        buyCartGoodsViewHolder.buycartitem_gooddesc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (cartGoodsBean.getCart_goodnum() == 1){
                    Toast.makeText(context,"最少选择一份",Toast.LENGTH_SHORT).show();
                }
                else if (cartGoodsBean.getCart_goodnum() > 1){
                    final int num = cartGoodsBean.getCart_goodnum()-1;
                    CartDescGoodsNumModel cartDescGoodsNumModel = new CartDescGoodsNumModel();
                    cartDescGoodsNumModel.cartDescGoodsNum(carid, new TListener<SuccessBean>() {
                        @Override
                        public void onResponse(SuccessBean successBean) {
                            if (successBean.getSuccess().equals("1")){
                                cartGoodsBean.setCart_goodnum(num);
                                buyCartGoodsViewHolder.buycartitem_goodsgmsl.setText(String.valueOf(num));
                                buyCartGoodsViewHolder.buycartitem_gjjsp.setText("您选择了"+String.valueOf(num)+"件商品");
                                buyCartGoodsViewHolder.buycartitem_goodsnum.setText(String.valueOf("X"+num));
                                buyCartGoodsViewHolder.buycartitem_goodsxiaojie.setText(String.valueOf(goodsprice*num));

                                Message message = Message.obtain();
                                message.what = 1;
                                handler.sendMessage(message);
                            }else{
                                Toast.makeText(context, "加载失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                        @Override
                        public void onFail(String msg) {
                            Toast.makeText(context, "加载失败1", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

            }
        });

        buyCartGoodsViewHolder.buycartitem_goodsadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final int num = cartGoodsBean.getCart_goodnum()+1;
                CartAddGoodsNumModel cartAddGoodsNumModel = new CartAddGoodsNumModel();
                cartAddGoodsNumModel.cartAddGoodsNum(carid, new TListener<SuccessBean>() {
                    @Override
                    public void onResponse(SuccessBean successBean) {
                        if (successBean.getSuccess().equals("1")){
                            cartGoodsBean.setCart_goodnum(num);
                            buyCartGoodsViewHolder.buycartitem_goodsgmsl.setText(String.valueOf(num));
                            buyCartGoodsViewHolder.buycartitem_gjjsp.setText("您选择了"+String.valueOf(num)+"件商品");
                            buyCartGoodsViewHolder.buycartitem_goodsnum.setText(String.valueOf("X"+num));
                            buyCartGoodsViewHolder.buycartitem_goodsxiaojie.setText(String.valueOf(goodsprice*num));
                            Message message = Message.obtain();
                            message.what = 1;
                            handler.sendMessage(message);
                        }else{
                            Toast.makeText(context, "加载失败", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onFail(String msg) {
                        Toast.makeText(context, "加载失败1", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });




    }
}
